/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.changeset;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.inspur.edp.bff.api.manager.IFSManager;
import com.inspur.edp.bff.entity.changeset.ViewModelChangeset;
import com.inspur.edp.cef.entity.changeset.IChangeDetail;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefEntityResInfoImpl;

import java.io.IOException;

public abstract class AbstractVMChangeConvertor extends JsonDeserializer<ViewModelChangeset> {

    @Override
    public ViewModelChangeset deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        ViewModelChangeset change = createChange();
        IChangeDetail changeDetail = getChangeDeserializer().readJson(jsonParser, deserializationContext);// .ReadJson(reader, objectType, existingValue, serializer);
        change.setChange(changeDetail);// = (IChangeDetail)((tempVar instanceof IChangeDetail) ? tempVar : null);
        return change;
    }

    /**
     * 创建对象实例方法，用来创建VM变更集数据实例。
     *
     * @return 得到的VM变更集数据实例
     * @see ViewModelChangeset
     */
    protected abstract ViewModelChangeset createChange();

    /**
     * 获取主节点变更集转换器
     *
     * @return 得到的根节点的变更集转换器
     * @see AbstractBffChangeJsonDeserializer
     */
    protected abstract AbstractBffChangeJsonDeserializer getChangeDeserializer();


    /**
     * 表单运行时调用
     *
     * @param ifsManager 视图对象FSManager管理器
     * @param nodeCode   视图对象节点编号
     * @return 实体资源信息
     * @see CefEntityResInfoImpl
     */
    protected CefEntityResInfoImpl cefEntityResInfo(IFSManager ifsManager, String nodeCode) {
        return (CefEntityResInfoImpl) ifsManager.getModelInfo().getCustomResource(nodeCode);
    }
}
